﻿using ESoft.DataAccess.EfCore.MySql;
using ESoft.Elasticsearch;
using ESoft.Locking;
using ESoft.Logging;
using ESoft.Service;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog;
using ESoft.RabbitMQ;
using ESoft.Redis;

namespace SampleWeb
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddEfCoreMySql(this.Configuration.GetConnectionString("ConnectionStrings"), typeof(Startup).Assembly);
            services.AddService();
            services.AddRedis("127.0.0.1:6379");
            services.AddRedisLock();
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            services.Configure<RabbitMQOption>(Configuration.GetSection("RabbitMQOption"));
            services.AddRabbitMQ(typeof(Startup).Assembly);
            services.AddNLoging();
            services.AddTransient<IEsService, EsService>();
            //services.AddZookeeper("127.0.0.1:2181");
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {

            //   LogManager.LoadConfiguration("Config/nlog.config");
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
            }
            app.UseMvc();
            app.ApplicationServices.UseRabbitMQConsumer(typeof(Startup).Assembly);
            app.ApplicationServices.UseNLog("Config/nlog.config");
        }
    }
}
